/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.CourseStudent;
import com.shanhesoft.java.modules.exam.domain.dto.CourseStudentDto;
import com.shanhesoft.java.modules.exam.domain.dto.CourseStudentQueryCriteria;
import com.shanhesoft.java.modules.exam.service.CourseStudentService;
import com.shanhesoft.java.modules.sys.domain.vo.UserVo;
import com.shanhesoft.java.modules.sys.service.UserService;

import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 课程学生信息Controller 课程学生信息
 * @author admin
 * @version 2020-09-25 23:37:51
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/courseStudent")
@Slf4j
@AllArgsConstructor
public class CourseStudentResource extends BaseResource {

	private final CourseStudentService service;

	private final UserService userService;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_courseStudent_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.findVoById(id));
	}
	/**
	 * GET / : get all courseStudent.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all courseStudent
	 */

	@PreAuthorize("@pms.hasPermission('exam_courseStudent_view')")
	@GetMapping
	@ApiOperation(value = "课程学生信息查看")
	public Result getPage(PageModel pm, CourseStudentQueryCriteria courseStudentQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, courseStudentQueryCriteria);
		return Result.buildOkData(service.findPage(pm, wrapper));
	}

	/**
	 * POST / : Save a courseStudentDto.
	 *
	 * @param courseStudentDto the HTTP courseStudent
	 */
	@PreAuthorize("@pms.hasPermission('exam_courseStudent_edit')")
	@ApiOperation(value = "课程学生信息编辑")
	@PostMapping
	public Result save(@Valid @RequestBody CourseStudentDto courseStudentDto) {
		log.debug("REST request to save CourseStudentDto : {}", courseStudentDto);

		// 通过学号获取ID
		UserVo userVo = userService.findVoByUsername(courseStudentDto.getUsername());
		if(null == userVo) {
			return Result.buildFail("学号对应学生信息不存在");
		}

		courseStudentDto.setStudentId(userVo.getId());
		service.saveOrUpdate(courseStudentDto);
		return Result.buildOk("保存课程学生信息成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" CourseStudent.
	 *
	 * @param ids the id of the courseStudent to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_courseStudent_del')")
	@ApiOperation(value = "课程学生信息删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete CourseStudent: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除课程学生信息成功");
	}

	@ApiOperation(value = "课程学生文件上传")
	@PostMapping(value = "/insertStudentList")
	public Result insertStudentList(@RequestBody Map<String, Object> obj){
		List<Map<String, Object>> list = (List<Map<String, Object>>) obj.get("params");
		List<CourseStudent> studentList = new ArrayList<CourseStudent>();
		for(Map<String, Object> map : list) {
			CourseStudent courseStudent = new CourseStudent();
			// 通过学号获取ID
			UserVo userVo = userService.findVoByUsername((String)map.get("username"));
			courseStudent.setStudentId(userVo.getId());
			courseStudent.setCourseId((String)map.get("courseId"));
			courseStudent.setUsername((String)map.get("username"));
			courseStudent.setNickname(userVo.getNickname());
			studentList.add(courseStudent);
		}

		service.saveOrUpdateBatch(studentList);
		return Result.buildOk("学生上传成功");
	}

	/**
	 * @param courseId
	 * @return
	 */
	@GetMapping("/getStudentCount")
	public Result getStudentCount(@Valid String courseId) {
		log.debug("REST request to get Entity : {}", courseId);
		QueryWrapper<CourseStudent> wrapper = new QueryWrapper<CourseStudent>();
		wrapper.eq("course_id", courseId);
		return  Result.buildOkData(service.count(wrapper));
	}
}
